package com.jifeng.br.pjj.bookreader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jifeng.br.pjj.bookreader.entity.*;
import com.jifeng.br.pjj.bookreader.mapper.BookMapper;
import com.jifeng.br.pjj.bookreader.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 图书表 服务实现类
 * </p>
 *
 * @author 机蜂
 * @since 2023-07-11
 */
@Service
@Transactional
public class BookServiceImpl extends ServiceImpl<BookMapper, BookEntity> implements BookService {

    @Autowired
    private BookCategoryService bookCategoryService;
    @Autowired
    private BookImageService bookImageService;
    @Autowired
    private BookLikeService bookLikeService;
    @Autowired
    private BookCommentService bookCommentService;
    @Autowired
    private BookCatalogService bookCatalogService;
    @Autowired
    private BookMapper bookMapper;

    @Override
    public IPage<BookEntity> getPage(int page, int limit, BookEntity book) {
        return this.page(new Page<>(page, limit), new QueryWrapper<BookEntity>()
                .like(StringUtils.isNotEmpty(book.getBookName()), "book_name", book.getBookName())
                .eq(StringUtils.isNotEmpty(book.getAuthorName()), "author_name", book.getAuthorName())
                .eq(book.getBookStatus() != null, "book_status", book.getBookStatus())
                .eq(book.getStatus() != null, "status", book.getStatus())
                .eq(book.getAuthorId() != null, "author_id", book.getAuthorId())
                .orderByDesc("book_id")
        );
    }

    @Override
    public void update(BookEntity book) {
        if (book.getBookId() == null) {
            this.save(book);//添加图书
        } else {
            this.updateById(book);
            deleteInfo(book.getBookId());
        }

        /*处理分类*/
        List<BookCategoryEntity> bcList = new ArrayList<>();
        for (Integer categoryId : book.getCategoryIds()) {
            BookCategoryEntity bc = new BookCategoryEntity();
            bc.setBookId(book.getBookId());
            bc.setCategoryId(categoryId);
            bcList.add(bc);
        }
        /*批量添加分类关联信息*/
        bookCategoryService.saveBatch(bcList);
        /*处理图片*/
        List<BookImageEntity> imageList = new ArrayList<>();
        for (String imageSrc : book.getImages()) {
            BookImageEntity bi = new BookImageEntity();
            bi.setBookId(book.getBookId());
            bi.setSrc(imageSrc);
            imageList.add(bi);
        }
        bookImageService.saveBatch(imageList);
    }

    @Override
    public void delete(int bookId) {
        this.removeById(bookId);
        deleteInfo(bookId);
        bookLikeService.remove(new QueryWrapper<BookLikeEntity>().eq("book_id", bookId));
        bookCommentService.remove(new QueryWrapper<BookCommentEntity>().eq("book_id", bookId));
        bookCatalogService.remove(new QueryWrapper<BookCatalogEntity>().eq("book_id", bookId));
    }

    @Override
    public List<BookEntity> getHotList() {
        return bookMapper.getHotList();
    }

    @Override
    public IPage<BookEntity> getBookByCid(int page, int limit, int categoryId) {
        return bookMapper.getBookByCategoryId(new Page<>(page, limit), categoryId);
    }

    @Override
    public IPage<BookEntity> getBookByRecord(int page, int limit, int userId) {
        return bookMapper.getBookByRecord(new Page<>(page, limit), userId);
    }

    @Override
    public IPage<BookEntity> getBookByLike(int page, int limit, int userId) {
        return bookMapper.getBookByLike(new Page<>(page, limit), userId);
    }


    private void deleteInfo(int bookId) {
        /*删除关联分类*/
        bookCategoryService.remove(new QueryWrapper<BookCategoryEntity>().eq("book_id", bookId));
        /*删除关联图片*/
        bookImageService.remove(new QueryWrapper<BookImageEntity>().eq("book_id", bookId));
    }
}
